package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ClazzMapper clazzMapper;
    @Override
    public List<Student> findClazzById(Integer id) {

        return   studentMapper.findClazzById(id);
    }

    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Student> lists=studentMapper.page(studentQueryParam);
        lists.forEach(list->{
            Clazz clazz = clazzMapper.findById(list.getClazzId());
            list.setClazzName(clazz.getName());
        });

        Page<Student> page= (Page<Student>) lists;
        return new PageResult<>(page.getTotal(),page.getResult());

    }

    /**
     * 新增学员
     * @param student
     */
    @Override
    public void addstudent(Student student) {

        student.setCreateTime(LocalDateTime.now());
      //  log.error("创建成功：{}",student.getCreateTime());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.addstudent(student);
    }

    @Override
    public Student findById(Integer id) {
        return studentMapper.findById(id);
    }
    /*更新学员*/
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }
    /*删除学员*/
    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }
    /*学员违纪*/
    @Override
    public void disById(Integer id, Integer score) {
        Student s = studentMapper.findById(id);
        LocalDateTime up=LocalDateTime.now();
        Integer ssum= Integer.valueOf(s.getViolationScore())+score;
        Integer csum= Integer.valueOf(s.getViolationCount())+1;
        studentMapper.disById(id,ssum,csum,up);
    }
}
